export class RequestHelper {
    auth(userName: string, pwd: string) {

    }

    /**响应结果统一处理 */
    static handlerResponse(res: ResposeJson | null | undefined) {
        if (!res) {
            return ResponseBody.fromJS({
                code: 500,
                errorMessage: '获取数据出错！',
                success: false
            });
        }
        if (res.unAuthorizedRequest) {
            //未授权
            return ResponseBody.fromJS({
                code: 403,
                errorMessage: res.error ? res.error.message : '未授权'
            });
        } else {
            if (res.success) {
                return ResponseBody.fromJS({
                    code: 200,
                    data: res.result,
                    success: true
                });
            } else {
                return ResponseBody.fromJS({
                    code: 500,
                    errorMessage: res.error ? res.error.message : '获取数据出错！',
                    success: false
                });
            }
        }
    }

}

export class ResponseBody {
    success: boolean = false;
    code: number = 200;
    data: any;
    errorMessage: string = '';
    constructor(js?: any) {
        if (js !== undefined) {
            this.success = js['success'] !== undefined ? js['success'] : false;
            this.code = js['code'] !== undefined ? js['code'] : undefined;
            this.data = js['data'] !== undefined ? js['data'] : undefined;
            this.errorMessage = js['errorMessage'] !== undefined ? js['errorMessage'] : undefined;
        }
    }
    static fromJS(js: any) {
        return new ResponseBody(js);
    }

}

export class ResposeJson {

    result: any;
    targetUrl: string = '';
    success: boolean = false;
    error: ResposeErrorJson = new ResposeErrorJson();
    unAuthorizedRequest: boolean = true;

    constructor(js?: any) {
        if (js !== undefined) {
            this.result = js['result'] !== undefined ? js['result'] : undefined;
            this.targetUrl = js['targetUrl'] !== undefined ? js['targetUrl'] : undefined;
            this.success = js['success'] !== undefined ? js['success'] : undefined;
            this.error = js['error'] !== undefined ? js['error'] : undefined;
            this.unAuthorizedRequest = js['unAuthorizedRequest'] !== undefined ? js['unAuthorizedRequest'] : undefined;
        }
    }
    static fromJS(js: any) {
        return new ResposeJson(js);
    }



}

export class ResposeErrorJson {
    code: number = 0;
    message: string = 'Current user did not login to the application!';
    details: any;
    validationErrors: any;
}